package com.le.tester.leetcode;

import org.apache.tools.ant.util.SymbolicLinkUtils;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;

/**
 * createTime：2022/7/13 16:00
 * description：星星碰撞
 */
public class AsteroidCollision {

    public int[] asteroidCollision(int[] asteroids) {
        //借助了双端队列
        Deque<Integer> stack = new ArrayDeque<>();

        for (int aster : asteroids) {
            boolean alive = true;
            while (alive && aster < 0 && !stack.isEmpty() && stack.peek() > 0) {
                alive = stack.peek() < -aster;
                if (stack.peek() <= -aster) {
                    stack.pop();
                }
            }
            if (alive) {
                stack.push(aster);
            }
        }
        int size = stack.size();
        int[] ans = new int[size];
        for (int i = size - 1; i >= 0; i--) {
            ans[i] = stack.pop();
        }
        return ans;
    }

    public static void main(String[] args) {

        int[] ints = {10, 2, -5};
        System.out.println(Arrays.toString(new AsteroidCollision().asteroidCollision(ints)));
    }
}
